var express = require('express');
var multer = require('multer');
var deploy = require('./deploy');

var app = express();

app.use(express.static('public'));
app.use(multer({dest: './uploads/'}))

app.post('/', function (req, res) {
    var file = req.files && req.files.file && req.files.file.path;
    if (file === null || file === undefined) {
        res.json({
            code: 1,
            msg: 'file is required'
        });
        return;
    }
    var path = req.body.path || req.query.path;
    if (path === null || path === undefined) {
        res.json({
            code: 2,
            msg: 'path is required'
        });
        return;
    }
    deploy.deploy(__dirname + '/../' + file, path, function (error, stdout, stderr) {
        res.json({
            code: 0,
            file: file,
            path: path,
            error: error,
            stdout: stdout,
            stderr: stderr
        })
    });
});


var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});